#include "consumer_module.h"
#include <iostream>
#include <algorithm>

// 添加地址
bool ConsumerModule::addAddress(int user_id, const std::string& address) {
    addresses[user_id] = address;
    std::cout << "Address added successfully." << std::endl;
    return true;
}

// 更新地址
bool ConsumerModule::updateAddress(int user_id, const std::string& address) {
    for (auto& addr : addresses) {
        if (addr.first == user_id) {
            addr.second = address;
            std::cout << "Address updated successfully." << std::endl;
            return true;
        }
    }
    std::cerr << "Address update failed. User ID not found." << std::endl;
    return false;
}

// 删除地址
bool ConsumerModule::deleteAddress(int user_id) {
    auto find_iter = addresses.find(user_id);

    if (find_iter != addresses.end())
    {
        addresses.erase(find_iter);
        std::cout << "Address deleted successfully. " << user_id << std::endl;
    }
    else 
    {
        std::cout << "user Address not exist. user_id: " << user_id << std::endl;
    }

    return true;
}

// 查看地址
bool ConsumerModule::viewAddresses(int user_id, std::string& userAddr) {
    auto findIter = addresses.find(user_id);
    if (findIter != addresses.end()) 
    {
        userAddr = findIter->second;
        return true;
    }
    else
    {
        userAddr = "";
        return false;
    }
}